<?php
/**
 * 应用系统商户模块
 * 
 */
class CompanyAction extends CommonAction {
	
	public function _initialize() {
		
		parent::_initialize();
		parent::_initmenu();	
		Vendor('Common.Tree');
	}
	
	/**
	 * 列表
	 */
	public function index(){
		
		if(IS_AJAX){
			
			$desc    = $this->_get("orderDirection","trim","desc");;
			$order   = $this->_get("orderName","trim","addtime");
			$pageNum = $this->_get("pageNum","intval",C('PAGE_NUMS'));
			$nowPage = $this->_get("p","intval",1);
			
			$map   = array("pageNum"=>$pageNum, "p"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);
			$json  = sendRest("BDC/Company/page",$map);
			$data  = $json['data'];
			$count = $data['count'];
			
			$totalPage = ceil($count/$pageNum);
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
			
			foreach($data['list'] AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'       => $row['id'],
						'username' => $row['username'],
						'title'    => $row['title'],
						'realname' => $row['realname'],
						'mobile'   => $row['mobile'],
						'email'    => $row['email'],
						'deal'     => '
					 <a href="'.__URL__.'/role/company/'.$row['id'].'"   class="btn blue icn-only" title="商户角色"><i class="icon-group"></i></a>
					 <a href="'.__URL__.'/outlet/company/'.$row['id'].'" class="btn blue icn-only" title="商户门店"><i class="icon-trello"></i></a>
				     <a href="'.__URL__.'/admin/company/'.$row['id'].'"  class="btn blue icn-only" title="商户用户"><i class="icon-user-md"></i></a>'
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,"JSON");
		}else{
			$this->display();
		}
	}

	/**
	 * 添加
	 */
	public function add(){
		
		if(IS_POST){
				
			$json = sendRest("BDC/Company/add", $_POST, "POST");
			if($json['status'] == 1){
				wlog(1);
				$this->assign("jumpUrl",U('Company/index'));
				$this->success(L('cfg_form_tipmsg_addsucc'));
			}else{
				wlog(2,$json['info']);
				$this->error($json['info']);
			}
		}else{
				
			$this->display();
		}
	}

	/**
	 * 编辑
	 */
	public function edit(){

		$id = $this->_get("id","intval",0);
		
		if(IS_POST){
		
			$json = sendRest("BDC/Company/edit", $_POST, "POST");
			if($json['status'] == 1){
				wlog(1);
				$this->assign("jumpUrl",U('Company/index'));
				$this->success(L('cfg_form_tipmsg_editsucc'));
			}else{
				wlog(2,$json['info']);
				$this->error($json['info']);
			}
		}else{
		
			$model   = D("Company");
			$detail  = $model->search($id);
			$this->assign("detail",$detail);
		
			$this->display();
		}
	}
	
	/**
	 * 删除
	 */
	public function del(){
		
		
	}

	/**
	 * 商户角色列表
	 */
	public function role(){
		
		$company = $this->_get("company","intval",0);
		if(IS_AJAX){
				
			$desc    = $this->_get("orderDirection","trim","desc");;
			$order   = $this->_get("orderName","trim","addtime");
			$pageNum = $this->_get("pageNum","intval",C('PAGE_NUMS'));
			$nowPage = $this->_get("p","intval",1);
					
			$map   = array("company"=>$company, "pageNum"=>$pageNum, "p"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);
			$json  = sendRest("BDC/Role/page",$map);
			$data  = $json['data'];
			$count = $data['count'];

			$totalPage = ceil($count/$pageNum);
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
			$upd       = L('cfg_list_itf_btn_modify');
			$del       = L('cfg_list_itf_btn_delete');
			foreach($data['list'] AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'       => $row['id'],
						'name'     => $row['name'],
						'title'    => $row['title'],
						'addtime'  => date("Y-m-d",$row['addtime']),
						'deal'     => '
					 <a href="'.__URL__.'/roleEdit/id/'.$row['id'].'" class="btn blue icn-only" title="'.$upd.'"><i class="icon-edit"></i></a>
				     <a href="#" class="btn red icn-only delAlone" title="'.$del.'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a>'
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,"JSON");
		}else{
			
			$model   = D("Company");
			$detail  = $model->search($company);
			$this->assign("company",$detail);
			
			$this->display();
		}
	}
	
	/**
	 * 添加商户角色
	 */
	public function roleAdd(){
		
		$company = $this->_param("company");
		
		if(IS_POST){
			
			$json = sendRest("BDC/Role/add", $_POST, "POST");
			if($json['status'] == 1){
				wlog(1);
				$this->assign("jumpUrl",U('Company/role',"company=".$company));
				$this->success(L('cfg_form_tipmsg_addsucc'));
			}else{
				wlog(2,$json['info']);
				$this->error($json['info']);
			}
		}else{
		
			$model   = D("Company");
			$detail  = $model->search($company);
			$this->assign("company",$detail);
			
			$this->display();
		}
	}
	
	/**
	 * 修改商户角色
	 */
	public function roleEdit(){
		
		$company = $this->_param("company");
		$id      = $this->_get("id");
		
		if(IS_POST){
				
			$json = sendRest("BDC/Role/edit", $_POST, "POST");
			if($json['status'] == 1){
				wlog(1);
				$this->assign("jumpUrl",U('Company/role',"company=".$company));
				$this->success(L('cfg_form_tipmsg_editsucc'));
			}else{
				wlog(2,$json['info']);
				$this->error($json['info']);
			}
		}else{
		
			$model   = D("Company");
			$detail  = $model->searchRole($id);
			$this->assign("detail",$detail);
			
			$company = $model->search($detail['company']);
			$this->assign("company",$company);
				
			$this->display();
		}
	}
	
	/**
	 * 删除商户角色
	 */
	public function roleDel(){
	
		$id     = $this->_param("id"    ,"intval",0);
		$map    = array("id"=>$id);
		
		$json   = sendRest("BDC/Role/del",$map);
		if($json['status'] == 1){
			$this->success(L('cfg_form_tipmsg_delsucc'));
		}else{
			$this->success(L('cfg_form_tipmsg_delfail'));
		}
	}
	
	/**
	 * 商户用户列表
	 */
	public function admin(){
		
		$company = $this->_get("company","intval",0);
		if(IS_AJAX){
		
			$desc    = $this->_get("orderDirection","trim","desc");;
			$order   = $this->_get("orderName","trim","addtime");
			$pageNum = $this->_get("pageNum","intval",C('PAGE_NUMS'));
			$nowPage = $this->_get("p","intval",1);
				
			$map   = array("company"=>$company, "pageNum"=>$pageNum, "p"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);
			$json  = sendRest("BDC/Admin/page",$map);
			$data  = $json['data'];
			$count = $data['count'];
		
			$totalPage = ceil($count/$pageNum);
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
			$upd       = L('cfg_list_itf_btn_modify');
			$del       = L('cfg_list_itf_btn_delete');
			foreach($data['list'] AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'       => $row['id'],
						'outlet'   => $row['outlet'],
						'username' => $row['username'],
						'jobno'    => $row['jobno'],
						'realname' => $row['realname'],
						'mobile'   => $row['mobile'],
						'addtime'  => date("Y-m-d",$row['addtime']),
						'deal'     => '
			 		<a href="'.__URL__.'/adminEdit/id/'.$row['id'].'" class="btn blue icn-only" title="'.$upd.'"><i class="icon-edit"></i></a>
		     		<a href="#" class="btn red icn-only delAlone" title="'.$del.'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a>'
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,"JSON");
		}else{
				
			$model   = D("Company");
			$detail  = $model->search($company);
			$this->assign("company",$detail);
				
			$this->display();
		}
	
	}
	
	/**
	 * 添加商户用户
	 */
	public function adminAdd(){
		
		$company = $this->_param("company");
		
		if(IS_POST){
			
			$json = sendRest("BDC/Admin/add", $_POST, "POST");
			if($json['status'] == 1){
				wlog(1);
				$this->assign("jumpUrl",U('Company/admin',"company=".$company));
				$this->success(L('cfg_form_tipmsg_addsucc'));
			}else{
				wlog(2,$json['info']);
				$this->error($json['info']);
			}
		}else{
			
			$map    = array("company"=>$company);
			$json   = sendRest("BDC/Role/index",$map);
			$this->assign("role",$json["data"]);
			
			$json    = sendRest("BDC/Role/index",$map);
			$this->assign("role",$json["data"]);
			
			$model  = D("Company");
			$detail = $model->search($company);
			$this->assign("company",$detail);
			
			$this->display();
		}
	}
	
	/**
	 * 修改商户用户
	 */
	public function adminEdit(){
		
		$company = $this->_param("company");
		$id      = $this->_get("id");
		
		if(IS_POST){
			
			$json = sendRest("BDC/Admin/edit", $_POST, "POST");
			if($json['status'] == 1){
				wlog(1);
				$this->assign("jumpUrl",U('Company/admin',"company=".$company));
				$this->success(L('cfg_form_tipmsg_editsucc'));
			}else{
				wlog(2,$json['info']);
				$this->error($json['info']);
			}
		}else{
			
			$model   = D("Company");
			$detail  = $model->searchAdmin($id);
			$this->assign("detail",$detail);
			
			$map     = array("company"=>$detail['company']);
			$json    = sendRest("BDC/Role/index",$map);
			$this->assign("role",$json["data"]);
			
			$json    = sendRest("BDC/Role/index",$map);
			$this->assign("role",$json["data"]);
				
			$company = $model->search($detail['company']);
			$this->assign("company",$company);
			
			$this->display();
		}
	}
	
	/**
	 * 删除商户用户
	 */
	public function adminDel(){
	
	
	}
	
	/**
	 * 商户门店列表
	 */
	public function outlet(){
	
		$company = $this->_get("company","intval",0);
		if(IS_AJAX){
	
			$desc    = $this->_get("orderDirection","trim","desc");;
			$order   = $this->_get("orderName","trim","addtime");
			$pageNum = $this->_get("pageNum","intval",C('PAGE_NUMS'));
			$nowPage = $this->_get("p","intval",1);
				
			$map   = array("company"=>$company, "pageNum"=>$pageNum, "p"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);
			$json  = sendRest("BDC/Outlet/page", $map);
			$data  = $json['data'];
			$count = $data['count'];
	
			$totalPage = ceil($count/$pageNum);
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
			$upd       = L('cfg_list_itf_btn_modify');
			$del       = L('cfg_list_itf_btn_delete');
			
			foreach($data['list'] AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'       => $row['id'],
						'title'    => $row['title'],
						'tel'      => $row['tel'],
						'mobile'   => $row['mobile'],
						'email'    => $row['email'],
						'addtime'  => date("Y-m-d",$row['addtime']),
						'deal'     => '
			 		<a href="'.__URL__.'/outletEdit/id/'.$row['id'].'" class="btn blue icn-only" title="'.$upd.'"><i class="icon-edit"></i></a>
		    	    <a href="#" class="btn red icn-only delAlone" title="'.$del.'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a>'
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,"JSON");
		}else{
				
			$model   = D("Company");
			$detail  = $model->search($company);
			$this->assign("company",$detail);
				
			$this->display();
		}
	}
	
	/**
	 * 添加商户门店
	 */
	public function outletAdd(){
	
		$company = $this->_param("company");
	
		if(IS_POST){
				
			$json = sendRest("BDC/Outlet/add", $_POST, "POST");
			if($json['status'] == 1){
				wlog(1);
				$this->assign("jumpUrl",U('Company/outlet',"company=".$company));
				$this->success(L('cfg_form_tipmsg_addsucc'));
			}else{
				wlog(2,$json['info']);
				$this->error($json['info']);
			}
		}else{
	
			$model   = D("Company");
			$company = $model->search($company);
			$this->assign("company",$company);
				
			$this->display();
		}
	}
	
	/**
	 * 修改商户门店
	 */
	public function outletEdit(){
	
		$company = $this->_param("company");
		$id      = $this->_get("id");
	
		if(IS_POST){
	
			$json = sendRest("BDC/Outlet/edit", $_POST, "POST");
			if($json['status'] == 1){
				wlog(1);
				$this->assign("jumpUrl",U('Company/outlet',"company=".$company));
				$this->success(L('cfg_form_tipmsg_editsucc'));
			}else{
				wlog(2,$json['info']);
				$this->error($json['info']);
			}
		}else{
	
			$model   = D("Company");
			$detail  = $model->searchOutlet($id);
			$this->assign("detail",$detail);
				
			$company = $model->search($detail['company']);
			$this->assign("company",$company);
	
			$this->display();
		}
	}
	
	/**
	 * 删除商户门店
	 */
	public function outletDel(){
		
		$id     = $this->_param("id"    ,"intval",0);
		$map    = array("id"=>$id);
		
		$json   = sendRest("BDC/Outlet/del",$map);
		if($json['status'] == 1){
			$this->success(L('cfg_form_tipmsg_delsucc'));
		}else{
			$this->success(L('cfg_form_tipmsg_delfail'));
		}
	}
	
	/**
	 * 列表节点
	 */
	public function node(){
	
		$list  = D('Permission')->listNode();
		$array = array();
	
		foreach($list as $k => $r) {
	
			$r['relation']= $r['relation']==1 ? '<font color="red">√</font>':'<font color="blue">×</font>';
				
			$sub         = $r['level'] > 1 ? 'href="#" class="btn"' : 'href="'.U("Company/nodeAdd","pid=".$r['id']).'" class="btn blue"';
			$r['sub']    = '<a '.$sub.' title="添加子节点" ><i class="icon-plus"></i></a>';
				
			$url         = U('Company/nodeEdit','id='.$r['id'].'&pid='.$r['pid']);
			$edit        = 'href="'.$url.'" class="btn blue"';
			$r['edit']   = '<a '.$edit.' title="修改节点" ><i class="icon-edit"></i></a>';
				
			$del         = $r['level']<1 ? 'class="btn"' : ' class="btn blue delAlone"';
			$r['del']    = '<a '.$del.'  title="删除节点" data-id="'.$r['id'].'"><i class="icon-remove"></i></a>';
			$array[]     = $r;
		}
	
		$str  = "<tr class='tr level_\$level'>
				    <td align='center'><input style='width:55px' type='text' value='\$sort' size='3' name='sort[\$id]'></td>
				    <td class='name'>\$spacer \$title</td>
					<td align='center'>\$url</td>
				    <td align='center'>\$relation</td>
					<td align='center'>
						\$sub \$edit \$del
					</td>
				  </tr>";
	
		$Tree = new Tree();
		$Tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
		$Tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		$Tree->init($array);
		$html_tree = $Tree->get_tree(0, $str);
	
		$this->assign('html_tree',$html_tree);
		$this->display();
	}
	
	/**
	 * 添加节点
	 */
	public function nodeAdd(){
	
		$model = D("Permission");
		if(IS_POST) {
	
			$json = sendRest("BDC/Node/add", $_POST, "POST");
			if($json['status'] == 1){
				wlog(1);
				$this->assign("jumpUrl",U('Company/node'));
				$this->success(L('cfg_form_tipmsg_addsucc'));
			}else{
				wlog(2,$json['info']);
				$this->error($json['info']);
			}
		}elseif(IS_AJAX){
			
			$module = $this->_get("module");
			$action = $this->_get("action","trim","");
			$pid    = $this->_get("pid","intval",0);
			if($module == $action){
				$action = "";
			}
			$nodes  = $model->listChild($pid,$module,$action);
			$this->ajaxReturn($nodes);
			
		}else{
			
			$pid    = $this->_get('pid','intval',0);
			$detail = $model->serach($pid);
			$this->assign('detail',$detail);
			
			if($detail['level'] == 0){
				$nodes = $model->listModule($pid);
				$this->assign('nodes',$nodes);
			}elseif($detail['level'] == 1){
				$nodes = $model->listAction($pid,$row['name']);
				$this->assign('nodes',$nodes);
			}

			$this->display();
		}
	}
	
	/**
	 * 编辑节点
	 */
	public function nodeEdit(){
	
		$model = D("Permission");
		if(IS_POST) {
				
			$json = sendRest("BDC/Node/edit", $_POST, "POST");
				
			if($json['status'] == 1){
				wlog(1);
				$this->assign("jumpUrl",U('Company/node'));
				$this->success(L('cfg_form_tipmsg_editsucc'));
			}else{
				wlog(2,$json['info']);
				$this->error($json['info']);
			}
		}else{
				
			$id  = $this->_get('id', 'intval',0);
			$pid = $this->_get('pid','intval',0);
			if(!$id){
				$this->error(L('cfg_form_tipmsg_illgel'));
			}
					
			$detail = $model->serach($id);
			$node   = $model->serach($detail['pid']);
			
			$this->assign('detail',$detail);
			$this->assign('node'  ,$node);
				
			$this->display();
		}
	}
	
	/**
	 * 删除节点
	 */
	public function nodeDel(){
	
		$id = $this->_param('id','intval',0);
		$id   = $this->_param('id','intval',0);
		$map  = array("id"=>$id);
		$json = sendRest("BDC/Node/del", $map);
	
		if($json['status'] == 1){
			$this->success(L("cfg_form_tipmsg_delsucc"));
		}else{
			$this->error(L("cfg_form_tipmsg_delfail"));
		}
	
	}
	
	/**
	 * 排序节点
	 */
	public function nodeSort(){
	
		$sorts = $this->_POST('sort');
		$map   = array("sort"=>$sorts);
		$json  = sendRest("BDC/Node/sort", $map, "POST");
	
		$this->success(L('nod_oth_sort_succ'));
	
	}
	
	/**
	 * 列表菜单
	 */
	public function menu(){
	
		$list  = D('Program')->listMenu();
		$array = array();
	
		foreach($list as $k => $r) {
	
			$r['type']  = $r['type']==1 ? '菜单':'快捷方式';
				
			$sub        = $r['level'] == 1 ? 'href="'.U("Company/menuAdd","pid=".$r['id']).'" class="btn blue"' : 'href="#" class="btn"' ;
			$r['sub']   = '<a '.$sub.' title="添加子菜单" ><i class="icon-plus"></i></a>';
				
			$url        = U('Company/menuEdit','id='.$r['id'].'&pid='.$r['pid']);
			$edit       = 'href="'.$url.'" class="btn blue"';
			$r['edit']  = '<a '.$edit.' title="修改菜单" ><i class="icon-edit"></i></a>';
				
			$r['del']   = '<a class="btn blue delAlone" '.$del.' title="删除菜单" data-id="'.$r['id'].'"><i class="icon-remove"></i></a>';
			$array[]    = $r;
		}
	
		$str  = "<tr class='tr level_\$level'>
				    <td align='center'><input style='width:55px' type='text' value='\$sort' size='3' name='sort[\$id]'></td>
				    <td class='name'>\$spacer \$title</td>
					<td align='center'>\$url</td>
				    <td align='center'>\$type</td>
					<td align='center'>
						\$sub \$edit \$del
					</td>
				  </tr>";
	
		$Tree = new Tree();
		$Tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
		$Tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		$Tree->init($array);
		$html_tree = $Tree->get_tree(0, $str);
	
		$this->assign('html_tree',$html_tree);
		$this->display();
	}
	
	/**
	 * 添加菜单
	 */
	public function menuAdd(){
	
		$model = D("Program");
		if(IS_POST) {
	
			$json = sendRest("BDC/Menu/add", $_POST, "POST");
				
			if($json['status'] == 1){
				wlog(1);
				$this->assign("jumpUrl",U('Company/menu'));
				$this->success(L('cfg_form_tipmsg_addsucc'));
			}else{
				wlog(2,$json['info']);
				$this->error($json['info']);
			}
		}else{
				
			$pid   = $this->_get('pid','intval',0);
			$list  = D("Permission")->listNode();
			$array = array();
			foreach($list as $k => $row) {
				if( $row['relation']== 2 || $row['level'] == 0 ){
					$row['disabled'] = 'disabled' ;
				}else{
					$row['disabled'] = '' ;
				}
				$array[$row['id']] = $row;
			}
			$str    = "<option value='\$id' url='\$url' \$selected \$disabled >\$spacer \$title</option>";
			$Tree   = new Tree();
			$Tree->init($array);
			$select = $Tree->get_tree(0, $str);
			$this->assign('options',$select);
				
			if($pid){
				$detail = $model->serach($pid);
			}else{
				$detail = array("level"=>0,"pid"=>0,"title"=>"顶级菜单");
			}
			$this->assign('detail',$detail);
	
			$this->display();
		}
	}
	
	/**
	 * 修改菜单
	 */
	public function menuEdit(){
	
		$model = D("Program");
		if(IS_POST) {
				
			$json = sendRest("BDC/Menu/edit", $_POST, "POST");
			if($json['status'] == 1){
				wlog(1);
				$this->assign("jumpUrl",U('Company/menu'));
				$this->success(L('cfg_form_tipmsg_editsucc'));
			}else{
				wlog(2,$json['info']);
				$this->error($json['info']);
			}	
		}else{
				
			$id  = $this->_get('id', 'intval',0);
			$pid = $this->_get('pid','intval',0);
			if(!$id){
				$this->error(L('cfg_form_tipmsg_illgel'));
			}
				
			$detail = $model->serach($id);
			$this->assign('detail',$detail);
			$config = json_decode($detail['config'],true);
			$this->assign('config',$config);
				
			$list  = D("Permission")->listNode();
			$array = array();
			foreach($list as $k => $row) {
				if( $row['relation']== 2 || $row['level'] == 0 ){
					$row['disabled'] = 'disabled' ;
				}
				if($detail['nodeid'] == $row['id'] ){
					$row['selected'] = 'selected' ;
				}
				$array[$row['id']] = $row;
			}
			$str    = "<option value='\$id' url='\$url' \$selected \$disabled >\$spacer \$title</option>";
			$Tree   = new Tree();
			$Tree->init($array);
			$select = $Tree->get_tree(0, $str, $detail['nodeid']);
			$this->assign('options',$select);
				
			$this->display();
		}
	}
	
	/**
	 * 删除菜单
	 */
	public function menuDel(){
	
		$id   = $this->_param('id','intval',0);
		$map  = array("id"=>$id);
		$json = sendRest("BDC/Menu/del", $map);
	
		if($json['status'] == 1){
			$this->success(L("cfg_form_tipmsg_delsucc"));
		}else{
			$this->error(L("cfg_form_tipmsg_delfail"));
		}
	}
	
	/**
	 * 排序菜单
	 */
	public function menuSort(){
	
		$sorts = $this->_POST('sort');
		$map   = array("sort"=>$sorts);
		$json  = sendRest("BDC/Menu/sort", $map, "POST", false);
	
		$this->success(L('nod_oth_sort_succ'));
	}

}